﻿Public Class FrmMantenimientoMaestroDetalleEliminar


    Private mMaestroBE As BE.Maestro
    Private mMaestroBS As BS.Maestro
    Private mListaDetalle As New List(Of Ejemplo.BE.Detalle)
    Private mId As Integer


    Public Property Id() As Integer
        Get
            Return mId
        End Get
        Set(ByVal value As Integer)
            mId = value
        End Set
    End Property

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        mMaestroBS = New BS.Maestro

        If MessageBox.Show("Desea eliminar los datos?", "Información", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then

            If (Not mMaestroBS.Eliminar(Id)) Then
                MessageBox.Show("Problemas al Eliminar")
            Else
                Me.DialogResult = Windows.Forms.DialogResult.OK
            End If

        End If
    End Sub


    Function DatosValidosMaestro() As Boolean
        Dim primerControl As Control = Nothing
        Dim mensajeDeValidacion As String = String.Empty

        Me.txtNombre.Text = Me.txtNombre.Text.Trim
        Me.txtDescripcion.Text = Me.txtDescripcion.Text.Trim


        If Me.txtNombre.Text = String.Empty Then
            mensajeDeValidacion = "Nombre es requerido"
            primerControl = Me.txtNombre
        End If


        If Me.txtDescripcion.Text = String.Empty Then
            mensajeDeValidacion &= vbCrLf & "Descripción es requerida"

            If primerControl Is Nothing Then
                primerControl = Me.txtDescripcion
            End If

        End If

        If Not primerControl Is Nothing Then
            MessageBox.Show(mensajeDeValidacion, "Validación", MessageBoxButtons.OK)
            primerControl.Focus()
            Return False

        End If

        Return True
    End Function


    Sub HabilitarControlesDetalle(ByVal Valor As Boolean)
        Me.txtNombreDetalle.Enabled = Valor
        Me.TxtDescripcionDetalle.Enabled = Valor
        Me.btnGuardarDetalle.Enabled = Valor
    End Sub


    Private Sub FrmMantenimientoMaestroDetalleInsertar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim detalleBS As New Ejemplo.BS.Detalle

        HabilitarControlesDetalle(False)
        mMaestroBS = New BS.Maestro
        mMaestroBE = mMaestroBS.Obtener(Id)

        Me.txtNombre.Text = mMaestroBE.Nombre.Trim
        Me.txtDescripcion.Text = mMaestroBE.Descripcion.Trim

        dtgDetalle.DataSource = Nothing
        dtgDetalle.DataSource = detalleBS.Obtener(mMaestroBE.Id)

        Me.txtNombre.Enabled = False
        Me.txtDescripcion.Enabled = False
        Me.HabilitarControlesDetalle(False)

    End Sub

    Private Sub dtgDetalle_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtgDetalle.SelectionChanged


        If dtgDetalle.SelectedRows.Count = 1 Then

            Dim dgvRow As DataGridViewRow

            For Each dgvRow In dtgDetalle.SelectedRows

                Me.txtNombreDetalle.Tag = dgvRow.Cells(0).Value
                Me.txtNombreDetalle.Text = dgvRow.Cells(2).Value.ToString.Trim
                Me.TxtDescripcionDetalle.Text = dgvRow.Cells(3).Value.ToString.Trim

            Next

        End If

    End Sub

    
End Class